import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

public class RedisDemoList {
    public static void main(String[] args) {
        // 1. 连接到 Redis 服务器上.
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        // 2. 从 Redis 连接池中取一个连接出来，并及时释放
        try(Jedis jedis = jedisPool.getResource()){
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);
        }

    }

    private static void test5(Jedis jedis) {
        System.out.println("llen");
        jedis.flushAll();

        jedis.rpush("key","111","222","333");

        long len = jedis.llen("key");
        System.out.println("length: "+len);


    }

    private static void test4(Jedis jedis) {
        System.out.println("blpop");
        jedis.flushAll();

        //返回结果是一个二元组，一个是从哪个key删除得到的结果，另一个是删除的元素是什么
        List<String> key = jedis.blpop(100, "key");
        System.out.println(key.get(0));
        System.out.println(key.get(1));

    }

    private static void test3(Jedis jedis) {
        System.out.println("lpop n rpop");
        jedis.flushAll();
        //头删
        jedis.rpush("key","111","222","333");
        String key = jedis.lpop("key");
        System.out.println(key);
        key = jedis.lpop("key");
        System.out.println(key);
        key = jedis.lpop("key");
        System.out.println(key);
        key = jedis.lpop("key");
        System.out.println(key);
        //尾删
        jedis.rpush("key","111","222","333");
        key = jedis.rpop("key");
        System.out.println(key);
        key = jedis.rpop("key");
        System.out.println(key);
        key = jedis.rpop("key");
        System.out.println(key);
        key = jedis.rpop("key");
        System.out.println(key);
    }

    private static void test2(Jedis jedis) {
        System.out.println("rpush");
        jedis.flushAll();

        jedis.rpush("key","111","222","333");

        List<String> list = jedis.lrange("key", 0, -1);
        System.out.println(list);
    }

    private static void test1(Jedis jedis) {
        System.out.println("lpush n lrange");
        jedis.flushAll();

        jedis.lpush("key","111","222","333");

        List<String> list = jedis.lrange("key", 0, -1);
        System.out.println(list);

    }
}
